package com.baozun.houji.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.base.BaseActivity
import com.baozhun.mall.common.ext.loadArrayListData
import com.baozhun.mall.common.ext.showToast
import com.baozhun.mall.common.util.StatusBarUtil
import com.baozun.houji.R
import com.baozun.houji.databinding.ActivityTestBinding
import com.baozun.houji.order.adapter.OrderListAdapter
import com.baozun.houji.viewmodel.RequestTestViewModel
import com.baozun.houji.viewmodel.TestViewModel
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import me.hgj.jetpackmvvm.ext.parseState

/**
 * @Title：test类
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2022/11/4
 * @Version：1.0.0
 */
@Route(path = RoutePath.Main.TEST)
class TestActivity : BaseActivity<TestViewModel, ActivityTestBinding>() {

    private val mRequestTestViewModel: RequestTestViewModel by viewModels()

    companion object {
        fun start(context: Context) {
            var intent = Intent(context, TestActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        //强烈注意：使用addLoadingObserve 将非绑定当前activity的viewModel绑定loading回调 防止出现请求时不显示 loading 弹窗bug
        addLoadingObserve(mRequestTestViewModel)
        mDataBinding.refresh.setOnRefreshListener {
            it.finishRefresh(true)
        }
        StatusBarUtil.setColor(this, ContextCompat.getColor(this, android.R.color.holo_red_dark), 0)
        mDataBinding.titleBar.setBackgroundColor(ContextCompat.getColor(this, android.R.color.holo_red_dark))
        mDataBinding.refresh.autoRefresh()
    }

    override fun initData() {
        super.initData()
        mRequestTestViewModel.login()
        mRequestTestViewModel.getOrderList()
        mViewModel.mImageUrl.value =
            "https://img2.woyaogexing.com/2019/08/30/abf3568adb8745ac9d5dc6cf9a3da895!600x600.jpeg"
    }

    override fun createObserver() {
        mRequestTestViewModel.loginResult.observe(this) {
            parseState(it, {
                showToast("请求成功")
            }, { it ->
                showToast(it.errorMsg)
            })
        }

        //通用的list请求回调处理
        mRequestTestViewModel.mOrderList.observe(this) {
            loadArrayListData(it, OrderListAdapter(arrayListOf()), SmartRefreshLayout(this))
        }
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            com.hjq.bar.R.id.title_bar_right_view -> showToast("点击了titleBar右边view")
            R.id.button -> showToast("点击了button")
        }
    }
}

